﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using QLKS.Templates;
using QLKS.SysLib;
using Infragistics.Windows.Ribbon;

namespace QLKS.Views
{
    /// <summary>
    /// Interaction logic for frmMain.xaml
    /// </summary>
    public partial class frmMain : FormRibbon
    {
        UserControl content = new UserControl();
        public delegate void EvnentLogOut();
        private event EvnentLogOut LogOut;

        #region 'Dependency Properties'
        public string UserName
        {
            get { return (string)GetValue(UserNameProperty); }
            set { SetValue(UserNameProperty, value); }
        }
        public string ServerName
        {
            get { return (string)GetValue(ServerNameProperty); }
            set { SetValue(ServerNameProperty, value); }
        }
        public string DataName
        {
            get { return (string)GetValue(DataNameProperty); }
            set { SetValue(DataNameProperty, value); }
        }

        public bool UnRegisted
        {
            get { return (bool)GetValue(UnRegistedProperty); }
            set { SetValue(UnRegistedProperty, value); }
        }

        // Using a DependencyProperty as the backing store for UnRegisted.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty UnRegistedProperty =
            DependencyProperty.Register("UnRegisted", typeof(bool), typeof(frmMain), new UIPropertyMetadata(true));
        public static readonly DependencyProperty UserNameProperty =
            DependencyProperty.Register("UserName", typeof(string), typeof(frmMain), new UIPropertyMetadata(""));
        public static readonly DependencyProperty ServerNameProperty =
            DependencyProperty.Register("ServerName", typeof(string), typeof(frmMain), new UIPropertyMetadata(""));
        public static readonly DependencyProperty DataNameProperty =
            DependencyProperty.Register("DataName", typeof(string), typeof(frmMain), new UIPropertyMetadata(""));
        #endregion  

        static public string _UserName = string.Empty;

        public frmMain()
        {
            InitializeComponent();
            LogOut += new EvnentLogOut(frmMain_LogOut);
            StatusBar.EventLogOut = LogOut;
        }

        //Đăng nhập
        private bool Login()
        {
            frmLogin frm = new frmLogin();
            frm.ShowDialog();
            UserName = frm.username;
            if (UserName == "")
                return false;
            //string[] Data = Models.DataProviderModel.conn.ConnectionString.Split(';')[1].Split('\\');
            _UserName = UserName;
            ServerName = ViewModels.LoginViewModel.ServerName;
            DataName = ViewModels.LoginViewModel.DatabaseName;
            LanguageLib.IsRegisted = !UnRegisted;
            return true;
        }

        //Đăng xuất
        private void frmMain_LogOut()
        {
            ViewModels.LoginViewModel.RememberLogin(false);
            this.Hide();
            if (!Login())
                this.Close();
            else
                this.Show();
        }

        private void btnSoDoPhong_Click(object sender, RoutedEventArgs e)
        {
            if (content.ToString() == "QLKS.Views.ucSoDoPhong")
                return;
            content = new ucSoDoPhong();
            mainContent.Children.Clear();
            mainContent.Children.Add(content);
            Reload_Language();
        }

        private void btnDmPhong_Click(object sender, RoutedEventArgs e)
        {
            if (content.ToString() == "QLKS.Views.ucDmPhong")
                return;
            content = new ucDmPhong();
            content.Name = "ucDmPhong";
            mainContent.Children.Clear();
            mainContent.Children.Add(content);
            Reload_Language();
        }

        private void btnLoaiPhong_Click(object sender, RoutedEventArgs e)
        {
            if (content.ToString() == "QLKS.Views.ucDmLoaiPhong")
                return;
            content = new ucDmLoaiPhong();
            content.Name = "ucDmLoaiPhong";
            mainContent.Children.Clear();
            mainContent.Children.Add(content);
            Reload_Language();
        }

        private void btnKhachHang_Click(object sender, RoutedEventArgs e)
        {
            if (content.ToString() == "QLKS.Views.ucDmKhachHang")
                return;
            content = new ucDmKhachHang();
            content.Name = "ucDmKhachHang";
            mainContent.Children.Clear();
            mainContent.Children.Add(content);
            Reload_Language();
        }

        private void btnDmTienTe_Click(object sender, RoutedEventArgs e)
        {
            if (content.ToString() == "QLKS.Views.ucDmTienTe")
                return;
            content = new ucDmTienTe();
            content.Name = "ucDmTienTe";
            mainContent.Children.Clear();
            mainContent.Children.Add(content);
            Reload_Language();
        }


        private void rtbEnglish_Click(object sender, RoutedEventArgs e)
        {
            RadioButtonTool mn = sender as RadioButtonTool;
            mn.IsChecked = true;
            rbtVietnam.IsChecked = mn.Name == "rbtVietnam" ? true : false;
            rtbEnglish.IsChecked = !rbtVietnam.IsChecked;
            LanguageLib.LanguageChange((bool)rbtVietnam.IsChecked ? "V" : "E");
            this.Reload_Language();
        }

        private void btnDsNhanVien_Click(object sender, RoutedEventArgs e)
        {
            if (content.ToString() == "QLKS.Views.ucDmNhanVien")
                return;
            content = new ucDmNhanVien();
            content.Name = "ucDmNhanVien";
            mainContent.Children.Clear();
            mainContent.Children.Add(content);
            Reload_Language();
        }

        private void btnDmNguoiDung_Click(object sender, RoutedEventArgs e)
        {
            if (content.ToString() == "QLKS.Views.ucDmNguoiDung")
                return;
            content = new ucDmNguoiDung();
            content.Name = "ucDmNguoiDung";
            mainContent.Children.Clear();
            mainContent.Children.Add(content);
            Reload_Language();
        }

        private void btnDangKyBanQuyen_Click(object sender, RoutedEventArgs e)
        {
            frmRegister frm = new frmRegister();
            frm.ShowDialog();
            UnRegisted = !frm.IsRegisted;
            LanguageLib.IsRegisted = frm.IsRegisted;
            if(frm.IsRegisted)
                Reload_Language();
        }

        private void btnNhomKhachHang_Click(object sender, RoutedEventArgs e)
        {
            if (content.ToString() == "QLKS.Views.ucNhomKhachHang")
                return;
            content = new ucNhomKhachHang();
            content.Name = "ucNhomKhachHang";
            mainContent.Children.Clear();
            mainContent.Children.Add(content);
            Reload_Language();
        }

        private void btnRestore_Click(object sender, RoutedEventArgs e)
        {
            frmRestore frm = new frmRestore();
            frm.Show();
        }

        private void btnBackup_Click(object sender, RoutedEventArgs e)
        {
            frmBackup frm = new frmBackup();
            frm.Show();
        }

        private void btnHistoryLogin_Click(object sender, RoutedEventArgs e)
        {
            frmLichSuDangNhap frm = new frmLichSuDangNhap();
            frm.Show();
        }

        private void btnNhomNhanVien_Click(object sender, RoutedEventArgs e)
        {
            if (content.ToString() == "QLKS.Views.ucNhomNguoiDung")
                return;
            content = new ucNhomNguoiDung();
            content.Name = "ucNhomNguoiDung";
            mainContent.Children.Clear();
            mainContent.Children.Add(content);
            Reload_Language();
        }

        //Load form
        private void FrmMain_Loaded(object sender, RoutedEventArgs e)
        {
            frmHotelWelcome frm = new frmHotelWelcome();
            frm.Show();
            frm.Topmost = true;
            try
            {

                if (System.IO.File.Exists("Key.Lic"))
                {
                    System.IO.StreamReader f = new System.IO.StreamReader("key.lic");
                    string id = ViewModels.RegisterViewModel.GetID();
                    string serial = f.ReadLine();
                    UnRegisted = !ViewModels.RegisterViewModel.Register(id, serial);
                    LanguageLib.IsRegisted = !UnRegisted;
                    this.Reload_Language();
                }
            }
            catch { }
            //System.Threading.Thread.Sleep(2000);
            if (M_LAN.Equals("V"))
                rbtVietnam.IsChecked = true;
            else
                rtbEnglish.IsChecked = true;
            frm.Close();
            ViewModels.LoginViewModel.RememberLogin(false);
            if (!Login())
                this.Close();
            btnSoDoPhong_Click(null, null);
        }

        //Báo cáo
        private void btnBCDoanhThuTheoLoaiPhong_Click(object sender, RoutedEventArgs e)
        {
            frmDKLocBCDoanhThuTheoLoaiPhong frm = new frmDKLocBCDoanhThuTheoLoaiPhong();
            frm.ShowDialog();
            if (!frm.flag)
                return;
            (new frmBCDoanhThuTheoLoaiPhong(frm.nam, frm.ky)).ShowDialog();
        }

        private void btnBCMatDoSuDungPhong_Click(object sender, RoutedEventArgs e)
        {
            frmDKLocBCDoanhThuTheoLoaiPhong frm = new frmDKLocBCDoanhThuTheoLoaiPhong();
            frm.ShowDialog();
            if (!frm.flag)
                return;
            (new frmBCMatDoSuDungPhong(frm.nam, frm.ky)).ShowDialog();
        }

        private void btnDmTuyChon_Click(object sender, RoutedEventArgs e)
        {
            if (content.ToString() == "QLKS.Views.ucDmTuyChon")
                return;
            content = new ucDmTuyChon();
            content.Name = "ucDmTuyChon";
            mainContent.Children.Clear();
            mainContent.Children.Add(content);
            Reload_Language();
        }

        private void btnTinNhan_Click(object sender, RoutedEventArgs e)
        {
            if (content.ToString() == "QLKS.Views.ucTinNhan")
                return;
            content = new ucTinNhan();
            content.Name = "ucTinNhan";
            mainContent.Children.Clear();
            mainContent.Children.Add(content);
            Reload_Language();
        }
    }
}
